Purpose

The purpose of this analysis document is to ensure the reproducability of the results by guiding the reader through the random forest analysis of the factors associated with the health of western redcedar.

Approach

Root data were shared by citizen scientists in the Wester Redcedar Dieback Map project on iNaturalist.

All of the data used in the below analyses are described in the Data Wrangle folder.

Limitations

Next steps

Data

The data used in the below visualizations are described in the Data Wrangle folder.

All tree health categories

## # A tibble: 11 x 2
## # Groups:   field.tree.canopy.symptoms [11]
##    field.tree.canopy.symptoms                             n
##    <fct>                                              <int>
##  1 Branch Dieback or 'Flagging'                          19
##  2 Browning Canopy                                       19
##  3 Extra Cone Crop                                        2
##  4 Healthy                                              403
##  5 Multiple Symptoms (please list in Notes)              17
##  6 New Dead Top (red or brown needles still attached)    33
##  7 Old Dead Top (needles already gone)                   83
##  8 Other (please describe in Notes)                       8
##  9 Thinning Canopy                                      118
## 10 Tree is dead                                          37
## 11 Yellowing Canopy                                      10

Filter Data

We need to filter the data to only include response and explanatory variables we’re interested in. For example, whether a sound clip was included in the iNat data is not important.

We also need to remove other response variables like “field.percent.canopy.affected….” so it is not used as a predictor for tree health.

Note it might be interesting to know if the user was an important factor in predicting if the tree is healthy/unhealthy.

There are also a number of factors that should probably be removed because they may be biasing the data. For example, only trees with the ‘other factor’ question may only be answered for unhealthy trees. We need to think about this a bit more.

Impute data

We continue to get the below error, but were able to work around it by imputing the data.

Error in randomForest.default(m, y, …) : Need at least two classes to do classification.

To impute the data we have to remove factors with >53 levels.

The below code lists the number of levels for the variables that are factors.

Imputed data table

## ntree      OOB      1      2      3      4      5      6      7      8      9     10     11
##   300:  45.53% 94.74% 94.74%100.00% 16.13% 70.59% 87.88% 73.49%100.00% 71.19% 91.89%100.00%
## ntree      OOB      1      2      3      4      5      6      7      8      9     10     11
##   300:  46.33% 94.74% 94.74%100.00% 16.38% 82.35% 87.88% 73.49%100.00% 72.88% 94.59%100.00%
## ntree      OOB      1      2      3      4      5      6      7      8      9     10     11
##   300:  45.93%100.00% 94.74%100.00% 15.38% 88.24% 87.88% 73.49%100.00% 72.03% 94.59%100.00%
## ntree      OOB      1      2      3      4      5      6      7      8      9     10     11
##   300:  46.06% 94.74% 94.74%100.00% 15.38% 76.47% 87.88% 75.90%100.00% 73.73% 94.59%100.00%
## ntree      OOB      1      2      3      4      5      6      7      8      9     10     11
##   300:  45.39% 94.74%100.00%100.00% 15.14% 76.47% 87.88% 69.88%100.00% 74.58% 91.89%100.00%
## ntree      OOB      1      2      3      4      5      6      7      8      9     10     11
##   300:  46.33% 94.74%100.00%100.00% 16.38% 82.35% 84.85% 74.70%100.00% 72.88% 91.89%100.00%

Train and test data

Try creating random forest model from training data

## 
## Call:
##  randomForest(formula = field.tree.canopy.symptoms ~ ., data = training,      ntree = 2001, importance = TRUE, proximity = TRUE, na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 2001
## No. of variables tried at each split: 23
## 
##         OOB estimate of  error rate: 45.28%
## Confusion matrix:
##                                                    Branch Dieback or 'Flagging'
## Branch Dieback or 'Flagging'                                                  1
## Browning Canopy                                                               0
## Extra Cone Crop                                                               0
## Healthy                                                                       1
## Multiple Symptoms (please list in Notes)                                      0
## New Dead Top (red or brown needles still attached)                            0
## Old Dead Top (needles already gone)                                           1
## Other (please describe in Notes)                                              0
## Thinning Canopy                                                               1
## Tree is dead                                                                  0
## Yellowing Canopy                                                              0
##                                                    Browning Canopy
## Branch Dieback or 'Flagging'                                     0
## Browning Canopy                                                  0
## Extra Cone Crop                                                  0
## Healthy                                                          3
## Multiple Symptoms (please list in Notes)                         1
## New Dead Top (red or brown needles still attached)               3
## Old Dead Top (needles already gone)                              1
## Other (please describe in Notes)                                 0
## Thinning Canopy                                                  0
## Tree is dead                                                     0
## Yellowing Canopy                                                 0
##                                                    Extra Cone Crop Healthy
## Branch Dieback or 'Flagging'                                     0       8
## Browning Canopy                                                  0      11
## Extra Cone Crop                                                  0       1
## Healthy                                                          0     265
## Multiple Symptoms (please list in Notes)                         0       7
## New Dead Top (red or brown needles still attached)               0      15
## Old Dead Top (needles already gone)                              0      26
## Other (please describe in Notes)                                 0       7
## Thinning Canopy                                                  0      45
## Tree is dead                                                     0      12
## Yellowing Canopy                                                 0       4
##                                                    Multiple Symptoms (please list in Notes)
## Branch Dieback or 'Flagging'                                                              0
## Browning Canopy                                                                           0
## Extra Cone Crop                                                                           0
## Healthy                                                                                   1
## Multiple Symptoms (please list in Notes)                                                  2
## New Dead Top (red or brown needles still attached)                                        0
## Old Dead Top (needles already gone)                                                       1
## Other (please describe in Notes)                                                          0
## Thinning Canopy                                                                           0
## Tree is dead                                                                              0
## Yellowing Canopy                                                                          0
##                                                    New Dead Top (red or brown needles still attached)
## Branch Dieback or 'Flagging'                                                                        0
## Browning Canopy                                                                                     3
## Extra Cone Crop                                                                                     0
## Healthy                                                                                             4
## Multiple Symptoms (please list in Notes)                                                            0
## New Dead Top (red or brown needles still attached)                                                  2
## Old Dead Top (needles already gone)                                                                 2
## Other (please describe in Notes)                                                                    0
## Thinning Canopy                                                                                     2
## Tree is dead                                                                                        3
## Yellowing Canopy                                                                                    0
##                                                    Old Dead Top (needles already gone)
## Branch Dieback or 'Flagging'                                                         1
## Browning Canopy                                                                      0
## Extra Cone Crop                                                                      1
## Healthy                                                                             12
## Multiple Symptoms (please list in Notes)                                             1
## New Dead Top (red or brown needles still attached)                                   2
## Old Dead Top (needles already gone)                                                 12
## Other (please describe in Notes)                                                     0
## Thinning Canopy                                                                     16
## Tree is dead                                                                         2
## Yellowing Canopy                                                                     0
##                                                    Other (please describe in Notes)
## Branch Dieback or 'Flagging'                                                      0
## Browning Canopy                                                                   0
## Extra Cone Crop                                                                   0
## Healthy                                                                           1
## Multiple Symptoms (please list in Notes)                                          0
## New Dead Top (red or brown needles still attached)                                0
## Old Dead Top (needles already gone)                                               0
## Other (please describe in Notes)                                                  0
## Thinning Canopy                                                                   0
## Tree is dead                                                                      0
## Yellowing Canopy                                                                  0
##                                                    Thinning Canopy Tree is dead
## Branch Dieback or 'Flagging'                                     2            0
## Browning Canopy                                                  0            0
## Extra Cone Crop                                                  0            0
## Healthy                                                         19            1
## Multiple Symptoms (please list in Notes)                         1            0
## New Dead Top (red or brown needles still attached)               2            1
## Old Dead Top (needles already gone)                             19            1
## Other (please describe in Notes)                                 0            0
## Thinning Canopy                                                 25            1
## Tree is dead                                                     5            0
## Yellowing Canopy                                                 3            0
##                                                    Yellowing Canopy class.error
## Branch Dieback or 'Flagging'                                      0   0.9166667
## Browning Canopy                                                   0   1.0000000
## Extra Cone Crop                                                   0   1.0000000
## Healthy                                                           0   0.1368078
## Multiple Symptoms (please list in Notes)                          0   0.8333333
## New Dead Top (red or brown needles still attached)                0   0.9200000
## Old Dead Top (needles already gone)                               0   0.8095238
## Other (please describe in Notes)                                  0   1.0000000
## Thinning Canopy                                                   0   0.7222222
## Tree is dead                                                      0   1.0000000
## Yellowing Canopy                                                  0   1.0000000

Concerns

Questions

Explore different groupings

Five filtered Symptoms

Selected tree health categories

## # A tibble: 5 x 2
## # Groups:   field.tree.canopy.symptoms [5]
##   field.tree.canopy.symptoms                             n
##   <fct>                                              <int>
## 1 Healthy                                              403
## 2 New Dead Top (red or brown needles still attached)    33
## 3 Old Dead Top (needles already gone)                   83
## 4 Thinning Canopy                                      118
## 5 Tree is dead                                          37
Train and test data
Try creating random forest model from training data
## 
## Call:
##  randomForest(formula = field.tree.canopy.symptoms ~ ., data = training,      ntree = 2001, importance = TRUE, proximity = TRUE, na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 2001
## No. of variables tried at each split: 23
## 
##         OOB estimate of  error rate: 42.18%
## Confusion matrix:
##                                                    Healthy
## Healthy                                                256
## New Dead Top (red or brown needles still attached)      14
## Old Dead Top (needles already gone)                     24
## Thinning Canopy                                         45
## Tree is dead                                            11
##                                                    New Dead Top (red or brown needles still attached)
## Healthy                                                                                             6
## New Dead Top (red or brown needles still attached)                                                  4
## Old Dead Top (needles already gone)                                                                 3
## Thinning Canopy                                                                                     1
## Tree is dead                                                                                        3
##                                                    Old Dead Top (needles already gone)
## Healthy                                                                             15
## New Dead Top (red or brown needles still attached)                                   5
## Old Dead Top (needles already gone)                                                 14
## Thinning Canopy                                                                     23
## Tree is dead                                                                         3
##                                                    Thinning Canopy Tree is dead
## Healthy                                                         16            5
## New Dead Top (red or brown needles still attached)               1            2
## Old Dead Top (needles already gone)                             23            2
## Thinning Canopy                                                 16            4
## Tree is dead                                                     7            2
##                                                    class.error
## Healthy                                              0.1409396
## New Dead Top (red or brown needles still attached)   0.8461538
## Old Dead Top (needles already gone)                  0.7878788
## Thinning Canopy                                      0.8202247
## Tree is dead                                         0.9230769

Binary symptoms

Binary tree health categories

## # A tibble: 2 x 2
## # Groups:   field.tree.canopy.symptoms [2]
##   field.tree.canopy.symptoms     n
##   <fct>                      <int>
## 1 Healthy                      403
## 2 Unhealthy                    346
Train and test data
Try creating random forest model from training data
## 
## Call:
##  randomForest(formula = field.tree.canopy.symptoms ~ ., data = training,      ntree = 2001, importance = TRUE, proximity = TRUE, na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 2001
## No. of variables tried at each split: 23
## 
##         OOB estimate of  error rate: 28.88%
## Confusion matrix:
##           Healthy Unhealthy class.error
## Healthy       221        82   0.2706271
## Unhealthy      80       178   0.3100775

Troubleshooting

Error in randomForest.default(m, y, …) : Need at least two classes to do classification.

I may be misunderstanding this error, but I think it is referring to the response variable?

The documentation here describes the error prompt when: if (classRF && !addclass && length(unique(y)) < 2) stop(“Need at least two classes to do classification.”)

It is possible some of the NA or -9999 values are causing issues.

We can try imputing the data, however this requires us to remove columns with more than 53 factors, which probably makes sense anyway.

Removing factors with more than 53 levels didn’t resolve the error from the randomForest command, but it did allow us to use the rfImpute command to impute our data.

Wow it actually worked if the data is imputed.